/**
 * 设置错误响应状态
 * @param errCode
 * @param errMsg
 * @returns {{msg: string, code: number, data: null}}
 */
exports.getErr = function (errCode = 500, errMsg = "server internal error") {
    return {
        code: errCode,
        msg: errMsg,
        data: null
    };
};

exports.getResult = function (result) {
    return {
        code: 200,
        msg: "请求成功",
        data: result,
    };
};

exports.asyncHandler = (handler) => {
    return async (req, res, next) => {
        try {
            const result = await handler(req, res, next);
            res.send(exports.getResult(result));
        } catch (err) {
            next(err);
        }
    };
};
